package com.lagou.rpc.consumer.task;

import com.lagou.rpc.consumer.zookeeper.ZkHelper;
import org.apache.curator.framework.CuratorFramework;

import java.util.List;

/**
 * @author boting.guo
 * @date 2021/11/14 12:33
 */

public class ScheduleTask implements Runnable {

	private CuratorFramework client;

	private ZkHelper zkHelper;

	public ScheduleTask(CuratorFramework client, ZkHelper zkHelper) {
		this.client = client;
		this.zkHelper = zkHelper;
	}

	@Override
	public void run() {
		try {
			List<String> strPath = client.getChildren().forPath("/server-registry");
			for (String s : strPath) {
				byte[] bytes = client.getData().forPath("/server-registry/" + s);
				String data = new String(bytes);
				String[] pieces = data.split(":");
				if (pieces.length != 2) {
					continue;
				}

				long lastTime = Long.parseLong(pieces[0]);
				if (System.currentTimeMillis() - lastTime > 5000) {
					zkHelper.getClientMap().get(s).setUsedTime(0);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
